const router = require('koa-router')();
router.prefix(appConfig.authPrefixUrl+'/desktop/sys_icon');
const SysIconService = appRequire('service/desktop/SysIconService.js');
const ResultVo = require(appConfig.rootUrl+'/vo/ResultVo.js');

router.post('/save',async(ctx)=>{
  try{
    let icon = ctx.request.body;
    let id = await SysIconService.save(icon);
    ctx.body = ResultVo.success('success',id);
  }catch(err){
    //集中处理错误
    err.msg = "新增图标失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/deleteOne',async(ctx)=>{
  try{
    let icon = ctx.query;
    icon = await SysIconService.deleteOne(icon.id);
    ctx.body = ResultVo.success('success',icon);
  }catch(err){
    //集中处理错误
    err.msg = "删除图标失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/deleteMany',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await SysIconService.deleteMany(param.ids);
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "删除图标失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findOne',async(ctx)=>{
  try{
    let icon = ctx.query;
    icon = await SysIconService.findOne(icon.id);
    console.log(icon);
    ctx.body = ResultVo.success('success',icon);
  }catch(err){
    //集中处理错误
    err.msg = "查找图标失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findAll',async(ctx)=>{
  try{
    let data = await SysIconService.findAll();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部图标数据失败!');
  }
});
router.get('/findByPage',async(ctx)=>{
  try{
    let page = ctx.query;
    console.log(page);
    let data = await SysIconService.findByPage(page);
    ctx.body = data;
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取分页图标数据失败!');
  }
});
module.exports = router;